/**
 * @file 
 * @author yuebin
 */
/*---------------------- example ------------------------
var SlideModal = require('tool/slideModal');
SlideModal.modal({
    modalClass: "",
    content: "",
    top: 100,
    afterOpen: function($modal) {
        // 对modal的处理
    },
    beforeClose: function($modal) {
    
    }
});

*/

define('tool/slideModal', function(require, exports, module) {
    'use strict';

    var $body = $('body');
    var prevModal = null;

    // 通过 exports 对外提供接口
    exports.modal = function(options) {
        // this.destory();
        if (typeof options !== 'object' || typeof options. content !== 'string') {
            return false;
        }
        prevModal && prevModal.destory();
        prevModal = new Modal(options);
    };

    // modal默认配置
    var defaultOptions = {
        content: '',
        top: 50
    };

    /**
     * Modal 类
     * @param {object} options 定义变量
     */
    function Modal(options) {
        var _this = this;
        this.options = $.extend(defaultOptions, options);

        this.$modal = $(etpl.compile(__inline('tpl/slideModal.html'))(this.options));
        this.$modalBody = this.$modal.find('.slide-modal-body');

        $body.append(this.$modal);

        // 调用用户定义的modal弹出层函数
        if (typeof options.afterOpen === 'function') {
            options.afterOpen(this.$modal);
        }

        // 关闭按钮
        this.$modal.on('click', '.slide-modal-close', function(e) {
            _this.$modal.destory();
            $('html').css({'overflow':'auto'});
        });

        /**
         * modal销毁
         */
        this.destory = function() {
            
            if (typeof this.options.beforeClose === 'function') {
                _this.options.beforeClose(this.$modal);
            }

            _this.$modal.off().removeData().remove();
            
        }

        // 对外提供销毁接口
        this.$modal.destory = function() {
            _this.$modal.animate({
                top: _this.options.top + _this.$modal.height()
            }, function() {
                _this.destory();
            });
        }

        /**
         * modal定位
         */
        var documentHeight = $(document).height();


        this.$modal.css('top', documentHeight).show();
        this.baseHeight = documentHeight - this.options.top;
        this.$modalBody.css({'overflow-y':'scroll','height':$(window).height()-200});
        $('html').css('overflow','hidden');
        //动画;
        this.$modal.animate({
            // top: this.options.top
            top:'50'
        }, function() {
            _this.modalAdapt();
        });

        this.modalAdapt = function() {
            var _height = this.$modalBody.height() + 100;
            if (_height > this.baseHeight) {
                _this.$modal.css('height', _height);
            }

            $('.slide-dark-bg').click(function(){
                $('.slide-modal-close').trigger('click');
            });
        };
    }

});